<?php

class AuthController extends App_Controller_Action
{
    public function loginzaAction()
    {
        $auth = Zend_Auth::getInstance();
        if (isset($_POST['token'])) {
            $adapter = new Core_Model_Auth_LoginzaAdapter($_POST['token']);
            $result = $auth->authenticate($adapter);
            if ($result->isValid()) {
                $identity = $result->getIdentity();
                $userModel = new Core_Model_User();
                $user = $userModel->login($identity);
                $auth->getStorage()->write($user->toArray());
                
            } else {
                $this->view->error_messages = $result->getMessages();
            }
        }

        if ($auth->hasIdentity()) {
            $this->view->user = $auth->getIdentity();
        }
        $return_url = 'http://' . $_SERVER['SERVER_NAME'] . $this->_helper->url->simple('loginza', 'auth', 'core');
        $this->view->return_url = urlencode($return_url);
        $this->render('login');
    }
}